package org.alexl.webhook.controller;

import org.alexl.webhook.domain.Message;
import org.alexl.webhook.domain.PushObject;
import org.alexl.webhook.exception.BadPasswordException;
import java.io.IOException;
import org.alexl.webhook.service.IWebHookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Gittee WebHook实现
 * 
 * 参见：
 *  @see http://git.mydoc.io/?t=154711
 * 
 * @author Alexander
 */
@RestController
@RequestMapping("49QbV9p6k7xrixWBqpo977HSDFZNV8DCE2fNy3qQ5btCQEi0If")
public class WebHookController {
    @Autowired
    private IWebHookService webHookService;
    
    @PostMapping(value = "push")
    public ResponseEntity<?> doPush(@RequestBody PushObject request) throws InterruptedException, IOException {
        try {
            boolean success = webHookService.execute(request);
            return new ResponseEntity<>(
                    new Message(String.valueOf(success), success ? "execute sucessfully" : "fail to execute"),
                    HttpStatus.OK);
        } catch (BadPasswordException ex) {
            return new ResponseEntity<>(
                    new Message("bad password", "password NOT match, please check the passsword again"),
                    HttpStatus.BAD_REQUEST);
        }
    }
    
    @PostMapping(value = "tag_push")
    public ResponseEntity<?> doTagPush() {
        return null;
    }
    
    @PostMapping(value = "issue")
    public ResponseEntity<?> doIssue() {
        return null;
    }
    
    @PostMapping(value = "pull_request")
    public ResponseEntity<?> doPullRequest() {
        return null;
    }
    
    @PostMapping(value = "comment")
    public ResponseEntity<?> doComment() {
        return null;
    }
    
}
